<?php
/**
 * 每日销售额报表
 * (model)
 */
namespace App\Models\SalesReport;

use App\Models\Basics\SysPlatAccount;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlat;
use Illuminate\Database\Eloquent\Model;

class DailyAmount extends Model{

    protected $table = 'sr_daily_amount';

    protected $guarded = [];

    public $timestamps = false;

    public function area(){
        return $this->belongsTo(SysArea::class, 'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class, 'country_id');
    }

    public function account(){
        return $this->belongsTo(SysPlatAccount::class,'account_id');
    }

    public function plat(){
        return $this->belongsTo(SysPlat::class,'plat_id','plat_id');
    }

    /**
     * 获取时间段内的每日销售额
     * @param $startTime
     * @param $endTime
     * @param $platId
     * @param $accountId
     * @param $countryId
     * @return array
     */
    public static function getDaysAmount($startTime, $endTime, $platId, $accountId, $countryId){
        return self::query()
            ->whereBetween('report_date', [$startTime, $endTime])
            ->where('plat_id', $platId)
            ->where('account_id', $accountId)
            ->where('country_id', $countryId)
            ->pluck('amount', 'report_date')
            ->toArray();
    }

    /**
     * 当日的销售额汇总
     * @param $reportDate
     * @param null $platId
     * @param null $accountId
     * @param null $countryId
     * @param null $areaId
     * @return int|mixed
     */
    public static function getDayAmountSum($reportDate, $platId = null, $accountId = null, $countryId = null, $areaId = null){
        $query = self::query()->where('report_date', $reportDate);
        if ($platId) $query->where('plat_id', $platId);
        if ($areaId) $query->where('area_id', $areaId);
        if ($accountId) $query->where('account_id', $accountId);
        if ($countryId) $query->where('country_id', $countryId);
        return $query->sum('amount');
    }

    /**
     * 获取时间段内的销售额汇总
     * @param $startTime
     * @param $endTime
     * @param null $platId
     * @param null $accountId
     * @param null $countryId
     * @param null $areaId
     * @return int|mixed
     */
    public static function getDaysAmountSum($startTime, $endTime, $platId = null, $accountId = null, $countryId = null, $areaId = null){
        $query = self::query()->whereBetween('report_date', [$startTime, $endTime]);
        if ($platId) $query->where('plat_id', $platId);
        if ($areaId) $query->where('area_id', $areaId);
        if ($accountId) $query->where('account_id', $accountId);
        if ($countryId) $query->where('country_id', $countryId);
        return $query->sum('amount');
    }
}
